//
//  TwentySevenViewController.m
//  项目二
//
//  Created by rimi on 16/5/17.
//  Copyright © 2016年 objective. All rights reserved.
//

#import "TwentySevenViewController.h"

@interface TwentySevenViewController ()
@property (nonatomic,strong)CALayer *animationLayer;
@end

@implementation TwentySevenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"动画、CALayer";
    /**CALayer是图层*/
    CALayer *animationLayer = [CALayer layer];
    animationLayer.frame = CRM(100, 100, 200, 300);
    animationLayer.backgroundColor = [UIColor orangeColor].CGColor;
    [self.view.layer addSublayer:animationLayer];
    self.animationLayer = animationLayer;
    
//    [self cabasicAnimation];
    
//    [self cakeyAnimation];
    
    [self animationGrop];
}
/**基础动画*/
-(void)cabasicAnimation{
    CABasicAnimation *basicAnimation = [CABasicAnimation animation];
    /**对于路劲进行动画*/
    basicAnimation.keyPath = @"path";
    /**设置动画时长*/
    basicAnimation.duration = 2.0f;
    /**动画重播次数*/
    basicAnimation.repeatCount = 1;
    /**动画是否回放*/
    basicAnimation.autoreverses = NO;
//    kCAFillModeBackwards 保持之前的状态
//    kCAFillModeForwards 保持之后的状态
    basicAnimation.fillMode = kCAFillModeForwards;
    /**保证在后台挂起时候动画保持持有*/
    basicAnimation.removedOnCompletion = NO;
    /**让动画3秒钟挂起后开始*/
    basicAnimation.beginTime = CACurrentMediaTime() + 1;
    
    /**属于绘图专用的layer*/
    CAShapeLayer  *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = self.animationLayer.bounds;
    
    /**贝赛尔曲线 用于绘制路线*/
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint:CPM(0, 0)];
    [bezierPath addLineToPoint:CPM(0, 300)];
    /**画曲线*/
    [bezierPath addQuadCurveToPoint:CPM(200, 300) controlPoint:CPM(100, 150)];
    [bezierPath addLineToPoint:CPM(200, 0)];
    [bezierPath addLineToPoint:CPM(0, 0)];
    /**将路径赋值绘图layer*/
    shapeLayer.path = bezierPath.CGPath;
    /**填充颜色*/
    shapeLayer.fillColor = [UIColor redColor].CGColor;
    /**路径颜色*/
    shapeLayer.strokeColor = [UIColor blackColor].CGColor;
    [self.animationLayer addSublayer:shapeLayer];
    /**最终达到的路径位置*/
    UIBezierPath *toPath = [UIBezierPath bezierPath];
    [toPath moveToPoint:CPM(0, 0)];
    [toPath addLineToPoint:CPM(0, 400)];
    [toPath addLineToPoint:CPM(200, 400)];
    [toPath addLineToPoint:CPM(200, 0)];
    [toPath addLineToPoint:CPM(0, 0)];
//    basicAnimation.byValue 针对动画前后位置相同的旋转类型，需要通过设置此值才有的动画效果  __bridge id  关联
    basicAnimation.toValue = (__bridge id)toPath.CGPath;
    [shapeLayer addAnimation:basicAnimation forKey:@""];
}

/**帧动画*/
- (void)cakeyAnimation{
   /**动画layer*/
    CALayer *layer = [CALayer layer];
    layer.frame = CRM(150, 400, 100, 30);
    layer.backgroundColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:layer];
    
    CAKeyframeAnimation *frameAnimation = [CAKeyframeAnimation animation];
    frameAnimation.keyPath = @"position.x";
    /**由于属于一帧一帧的动画 所以其动画运行值是以数组的形式来保存*/
    frameAnimation.values = @[@-30,@45,@-45,@15];
    frameAnimation.keyTimes = @[@0.5,@0.6,@0.9,@1];
    /**设置动画时长*/
    frameAnimation.duration = 0.75f;
    /**依照自身状态进行画值得改变*/
    frameAnimation.additive = YES;
    /**设置动画开始时间*/
    frameAnimation.beginTime = CACurrentMediaTime() + 1;
    [layer addAnimation:frameAnimation forKey:@""];
}
/**动画组*/
-(void)animationGrop{
    CAAnimationGroup *grop = [CAAnimationGroup animation];
    grop.duration = 2.0f;
    grop.beginTime = CACurrentMediaTime() + 1;
    
    CABasicAnimation *animationFirst = [CABasicAnimation animation];
    animationFirst.keyPath = @"transform.scale";
    animationFirst.toValue = @0.5;
    
    CABasicAnimation *animationSecond = [CABasicAnimation animation];
    animationSecond.keyPath = @"transform.rotation";
    animationSecond.byValue = @(M_PI * 2);
    
    grop.animations = @[animationSecond , animationFirst];
    [self.view.layer addAnimation:grop forKey:@""];

}
@end
